package com.sfang.common.base.entity;


import com.sfang.common.base.enums.ResultStatus;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by psalmody on 2016/8/8.
 */
public class RestInfo<T> implements Serializable {
    private boolean success;
    private String msg;
    private int status;

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    private long totalCount;
    private int code;
    private Date serverTime;
    private T data;
    private Object error;
    private String errorCode;
    private T response;
    private T errorResponse;

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    private Map<String, String> headers;


    public boolean isSuccess() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public T getErrorResponse() {
        return errorResponse;
    }

    public void setErrorResponse(T errorResponse) {
        this.errorResponse = errorResponse;
    }


    public RestInfo() {
    }

    public RestInfo(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
        this.serverTime = new Date();
    }

    public RestInfo(boolean success, int status, String msg) {
        this.success = success;
        this.status = status;
        this.msg = msg;
        this.serverTime = new Date();
    }

    public RestInfo(boolean success, int status, String msg, T data) {
        this.success = success;
        this.status = status;
        this.msg = msg;
        this.serverTime = new Date();
        this.data = data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean getSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getStatus() {
        return this.status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getServerTime() {
        return this.serverTime;
    }

    public void setServerTime(Date serverTime) {
        this.serverTime = serverTime;
    }

    public T getResponse() {
        return response;
    }

    public void setResponse(T response) {
        this.response = response;
    }

    public Object getError() {
        return this.error;
    }

    public void setError(Object error) {
        this.error = error;
    }

    public static <T> RestInfo<T> tryLockFailRes() {
        RestInfo restInfo = new RestInfo();
        restInfo.setSuccess(false);
        restInfo.setServerTime(new Date());
        restInfo.setStatus(ResultStatus.Fail.ordinal());
        restInfo.setErrorCode("406");

        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setCode(ResultStatus.BizError.getStatus());
        errorResponse.setSubCode(restInfo.getErrorCode());
        restInfo.setErrorResponse(errorResponse);

        return restInfo;
    }

    public static class InnerSuccess {
        private boolean isSuccess;

        public boolean getIsSuccess() {
            return isSuccess;
        }

        public void setIsSuccess(boolean success) {
            isSuccess = success;
        }

    }

    public static class ErrorResponse {
        private Integer code;
        private String subCode;
        private String message;

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getSubCode() {
            return subCode;
        }

        public void setSubCode(String subCode) {
            this.subCode = subCode;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }


    }
}
